package com.wqc.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ConsumerListener {


    @RabbitListener(queues = "springboot_queue")
    public void msg(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){

        System.out.println(System.currentTimeMillis()+"-消费者1接收到消息："+msg);
        //TODO 这里可以做异步的工作
        try {
            Thread.sleep(1000);
            channel.basicAck(tag, false);
            System.out.println(System.currentTimeMillis()+"-消费者1确认了消息："+msg);
        } catch (IOException e) {
            System.out.println("********************确认失败******************************");
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "springboot_queue")
    public void msg2(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){

        System.out.println(System.currentTimeMillis()+"-消费者2接收到消息："+msg);
        //TODO 这里可以做异步的工作
        /*try {
            Thread.sleep(20000);
            channel.basicAck(tag, false);
            System.out.println(System.currentTimeMillis()+"-消费者2确认了消息："+msg);
        } catch (IOException e) {
            System.out.println("********************确认失败******************************");
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }
}
